Sub Executable Data-Alignment Script

    Dim wsOrig      As Worksheet
    Dim wsNew       As Worksheet
    Dim lastRowGPS  As Long
    Dim lastRowAcc  As Long

    Dim gpsData()   As Variant
    Dim accData()   As Variant

    Dim tsGPS()     As Double
    Dim tsAcc()     As Double
    Dim sortedIdx() As Long

    Dim i As Long, j As Long
    Dim matchIdx As Long
    Dim outputData() As Variant

    
    Set wsOrig = ThisWorkbook.Sheets("  ") 'Enter worksheet name containing the data to align

    On Error Resume Next
    Set wsNew = ThisWorkbook.Sheets("Dati Allineati")
    If wsNew Is Nothing Then
        Set wsNew = ThisWorkbook.Sheets.Add
        wsNew.Name = "Dati Allineati"
    Else
        wsNew.Cells.Clear
    End If
    On Error GoTo 0

    lastRowGPS = wsOrig.Cells(wsOrig.Rows.Count, "A").End(xlUp).Row
    lastRowAcc = wsOrig.Cells(wsOrig.Rows.Count, "L").End(xlUp).Row

    gpsData = wsOrig.Range("A2:K" & lastRowGPS).Value

    accData = wsOrig.Range("L2:P" & lastRowAcc).Value

    Dim nGPS As Long: nGPS = lastRowGPS - 1
    Dim nAcc As Long: nAcc = lastRowAcc - 1

    ReDim tsGPS(1 To nGPS)
    ReDim tsAcc(1 To nAcc)
    ReDim sortedIdx(1 To nAcc)

    For i = 1 To nGPS
        
        tsGPS(i) = CDbl(CDate(gpsData(i, 3)) + CDate(gpsData(i, 4)))
    Next i

    For i = 1 To nAcc
        tsAcc(i) = CDbl(CDate(accData(i, 1)) + CDate(accData(i, 2)))
        sortedIdx(i) = i
    Next i

    Dim tempTs  As Double
    Dim tempIdx As Long
    For i = 2 To nAcc
        tempTs = tsAcc(i)
        tempIdx = sortedIdx(i)
        j = i - 1
        Do While j >= 1 And tsAcc(j) > tempTs
            tsAcc(j + 1) = tsAcc(j)
            sortedIdx(j + 1) = sortedIdx(j)
            j = j - 1
        Loop
        tsAcc(j + 1) = tempTs
        sortedIdx(j + 1) = tempIdx
    Next i

    ReDim outputData(1 To nGPS, 1 To 14)

    For i = 1 To nGPS

        Dim col As Long
        For col = 1 To 11
            outputData(i, col) = gpsData(i, col)
        Next col

        matchIdx = BinarySearchClosest(tsAcc, nAcc, tsGPS(i))

        
        If matchIdx > 0 Then
            Dim origIdx As Long
            origIdx = sortedIdx(matchIdx)
            outputData(i, 12) = accData(origIdx, 3)
            outputData(i, 13) = accData(origIdx, 4)
            outputData(i, 14) = accData(origIdx, 5)
        End If
        

    Next i

    
    wsNew.Range("A1:K1").Value = wsOrig.Range("A1:K1").Value
    wsNew.Range("L1:N1").Value = wsOrig.Range("N1:P1").Value

    
    wsNew.Range("A2").Resize(nGPS, 14).Value = outputData

    MsgBox "Allineamento completato!", vbInformation

End Sub


Private Function BinarySearchClosest(tsAcc() As Double, _
                                     nAcc As Long, _
                                     target As Double) As Long
    Dim lo As Long, hi As Long, mid As Long
    Dim bestIdx As Long

    lo = 1
    hi = nAcc
    bestIdx = 0

    Do While lo <= hi
        mid = (lo + hi) \ 2

        If tsAcc(mid) >= target Then
            bestIdx = mid
            hi = mid - 1
        Else
            lo = mid + 1
        End If
    Loop

    BinarySearchClosest = bestIdx
End Function

